<?php
/**
 * Created by PhpStorm.
 * User: denh
 * Date: 16/6/15
 * Time: 下午5:36
 */

class automation extends Base {

    public static $hproseHttpClient = null;
    public $phones = array(
        //'15600123661', #邓汇
        //'15201330722', #郑威颂
    );

    public $phones_mange = array(
        //'13910639396', #杨惠茹
        '13717864750', #刘辉
        //'18810132766', #王冠橉
        //'13811222321', #李若宁
        //'15811038802', #马东方
        //'18810863696', #张浩
        //'18676661341', #俞晓炀
        '18910184804', #许春思
        '18600879080', #朱宏伟
        //'15810056939', #姜丽婷
        //'13911527639', #李总
        //'13220165555', #王杨
    );

    function __construct() {
        parent::__construct();
        if (G_SERV_ID == 14) {
            $this->phones = array_merge($this->phones, $this->phones_mange);
        }
    }

    function load_hprose($base_url = USER_CENTER_BASE_URL) {
        if (self::$hproseHttpClient == null) {
            require_once __DIR__ . '/../sdk/hprose-php-master/src/Hprose.php';
            self::$hproseHttpClient = new \HproseHttpClient($base_url);
        }
    }

    function f发送用户数到手机() {
        $doUser = new Do用户();
        $user_total = $doUser->where("unsubscribe = 0 and 公众号ID=".G_SERV_ID)->count();
        $end = date("Y-m-d H:i:s", time());
        $date = substr($end, 0, 10);
        $minute = substr($end, 11, 5);
        $start = $date . ' ' . '00:00:00';
        $newAdd = $doUser->where("公众号ID=".G_SERV_ID." and unsubscribe=0 and regist_time  BETWEEN '" . $start . "'  AND '" . $end . "'")->count();
        $day = date("Y年m月d日", time());
        $content = $day."-爱贝儿医公号总用户数：（".$user_total."），截止到".$minute."，当日新增共计：（".$newAdd."）";
        foreach($this->phones as $v){
            $this->send_text_messages($content, $v);
        }
    }

    function send_text_messages($content, $tel){
        $post_data = array();
        $post_data['userid'] = '6474';
        $post_data['password'] = 'qwer1234';
        $post_data['account'] ='a10498';
        $post_data['content'] = $content;
        $post_data['mobile'] = $tel;
        $post_data['sendtime'] = '';
        $url='http://121.52.209.124:8888/sms.aspx?action=send';
        $o='';
        foreach ($post_data as $k=>$v) {
            $o.="$k=".urlencode($v).'&';
        }
        $post_data=substr($o,0,-1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        logger()->logInfo("发送短信给" . $tel. "，内容：". $content);
        $doNoteRecord = new DoNote_record();
        $doNoteRecord->set_tel($tel);
        $doNoteRecord->set_content($content);
        $doNoteRecord->set_time(date('Y-m-d H:i:s',time()));
        $doNoteRecord->save();
    }

    function send_messages($content, $tel){
        $post_data = array();
        $post_data['userid'] = '6948';
        $post_data['password'] = '147258';
        $post_data['account'] ='y00604';
        $post_data['content'] = $content;
        $post_data['mobile'] = $tel;
        $post_data['sendtime'] = '';
        $url='http://121.52.209.124:8888/sms.aspx?action=send';
        $o='';
        foreach ($post_data as $k=>$v) {
            $o.="$k=".urlencode($v).'&';
        }
        $post_data=substr($o,0,-1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        logger()->logInfo("发送短信给" . $tel. "，内容：". $content);
    }

    function get_area() {
        $doUser = new Do用户();
        $list = $doUser->fields('id, telephone')->where("!(telephone is null) and province = ''")->rows();
        foreach ($list as $k => $v) {
            $re = $this->get_area_by_phone($v['telephone']);
            if ($re) {
                if (!$v['id']) continue;
                $do = new Do用户();
                $do->set_id($v['id']);
                $do->set_province($re['result']['province']);
                $do->set_city($re['result']['city']);
                $do->update();
            }
        }
    }

    function get_userCenter_info() {
        $this->load_hprose();
        $doUser = new Do用户();
        $list = $doUser->fields('id, openId, telephone')->where("unsubscribe = 0 and userCenterID = ''")->rows();
        foreach ($list as $k => $v) {
            $openId = $v['openId'];
            $phone = $v['telephone'] ? $v['telephone'] : '';
            $re = self::$hproseHttpClient->UserCenterController_wxGetUserInfo($openId, $phone);
            if (isset($re->result->account->id)) {
                if (!$v['id']) continue;
                $do = new Do用户();
                $do->set_id($v['id']);
                $do->set_userCenterID($re->result->account->id);
                $do->update();
            }
        }
    }

    function updateOpenID() {
        $doDoc = new Do医师详情();
        $docs = $doDoc->where("openID = ''")->rows();
        foreach ($docs as $k => $v) {
            $doUser = new Do用户();
            $openID = $doUser->fields("openID")->where("id = ". $v['用户id'])->getOneField();
            if ($openID) {
                $doDoc1 = new Do医师详情();
                $doDoc1->set_id($v['id']);
                $doDoc1->set_openID($openID);
                $doDoc1->save();
            }
        }
    }

    function get_area_by_phone($phone) {
        header('Content-type:text/html;charset=utf-8');
        $apiurl = 'http://apis.juhe.cn/mobile/get';
        $params = array(
            'key' => '1460069ec0890a8e3d61f3202ecd66ab', //您申请的手机号码归属地查询接口的appkey
            'phone' => $phone //要查询的手机号码
        );

        $paramsString = http_build_query($params);

        $content = @file_get_contents($apiurl.'?'.$paramsString);
        $result = json_decode($content,true);
        if($result['error_code'] == '0'){
            return $result;
            /*
            "province":"浙江",
            "city":"杭州",
            "areacode":"0571",
            "zip":"310000",
            "company":"中国移动",
            "card":"移动动感地带卡"

            echo "省份：".$result['result']['province']."\r\n";
            echo "城市：".$result['result']['city']."\r\n";
            echo "区号：".$result['result']['areacode']."\r\n";
            echo "邮编：".$result['result']['zip']."\r\n";
            echo "运营商：".$result['result']['company']."\r\n";
            echo "类型：".$result['result']['card']."\r\n";
            */
        } else {
            return false;
        }
    }


}